﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Resn.WhuPub.BLL;

namespace Resn.WhuPub.WPFClientUI
{
        /// <summary>
        /// CreateBranchWindow.xaml 的交互逻辑
        /// </summary>
        public partial class CreateBranchWindow
        {
                private readonly GlobalManager _gm = GlobalManager.Instance;

                public CreateBranchWindow()
                {
                        InitializeComponent();
                }

                private void Window_Loaded(object sender, RoutedEventArgs e)
                {
                        _gm.Name = string.Empty;
                        _gm.Province = _gm.Provinces.First();
                        _gm.Address = string.Empty;
                        _gm.Postcode = string.Empty;
                        _gm.Website = string.Empty;

                        LayoutRoot.DataContext = _gm;

                }

                private void Window_OnMouseDown(object sender, MouseButtonEventArgs e)
                {
                        if (e.ChangedButton == MouseButton.Left)
                        {
                                this.DragMove();
                        }
                }

                private void AcceptButton_Click(object sender, RoutedEventArgs e)
                {
                        if (_gm.Corporate == null)
                        {
                                MessageBox.Show("学校不能为空");
                                return;
                        }

                        if (String.IsNullOrEmpty(_gm.Name))
                        {
                                MessageBox.Show("校区名称不能为空");
                                return;
                        }

                        try
                        {
                                Branch branch = _gm.Corporate.CreateBranch(_gm.Name,
                                                                           _gm.Address,
                                                                           _gm.Postcode,
                                                                           _gm.Province.Name,
                                                                           _gm.City.Name,
                                                                           _gm.Website);
                                branch.Insert();

                                _gm.Branchs.Add(branch);
                        }
                        catch (Exception ex)
                        {
                                MessageBox.Show(ex.Message);
                                return;
                        }

                        finally
                        {
                        }

                        Close();

                }
        }
}